{include file="Layout/header"/}
<body>
<div class="layui-fluid">
  <div class="layui-row layui-col-space15">
    <div class="layui-col-sm12 layui-col-md12 layui-col-lg12">
      <div class="layui-card">
        <div class="layui-card-body">
          <div class="layui-form toolbar">
            <div class="layui-form-item">
              <div class="layui-inline">
                <input id="roleName" class="layui-input layui-input-inline" type="text" placeholder="角色名称"/>
              </div>
              <div class="layui-inline">
                <button id="btnSearch" class="layui-btn layui-btn-radius layui-btn-sm icon-btn"><i class="layui-icon">&#xe615;</i>搜索</button>
                <button id="btnExp" class="layui-btn layui-btn-radius layui-btn-sm icon-btn"><i class="layui-icon">&#xe67d;</i>导出</button>
              </div>
            </div>
          </div>
          <table id="roleTable" lay-filter="roleTable"></table>
        </div>
      </div>
    </div>
  </div>
</div>
<script type="text/html" id="tableToolBar">
  {php} if(in_array("admin/sysrole/add",session('menunames'))){ {/php}
  <a class="layui-btn layui-btn-normal layui-btn-sm layui-btn-radius" lay-event="add"><i class="layui-icon">&#xe654;</i>添加</a>
  {php} } {/php}
  {php} if(in_array("admin/sysrole/edit",session('menunames'))){ {/php}
  <a class="layui-btn layui-btn-normal layui-btn-sm layui-btn-radius" lay-event="edit"><i class="layui-icon">&#xe642;</i>修改</a>
  {php} } {/php}
  {php} if(in_array("admin/sysrole/delete",session('menunames'))){ {/php}
  <a class="layui-btn layui-btn-danger layui-btn-sm layui-btn-radius" lay-event="del"><i class="layui-icon">&#xe640;</i>删除</a>
  {php} } {/php}
</script>
{include file="Layout/commonjs"/}
<script>
    layui.use(['layer', 'form', 'table', 'formSelects', 'dtree'], function () {
        //一般直接写在一个js文件中
        //var layer = layui.layer;
        //var form = layui.form;
        var table = layui.table;
        //var element = layui.element;
        table.render({
            elem: "#roleTable",
            id: 'roleTable',
            url: '{:url("/admin/sysrole/view")}',
            method: "post",
            request: {
                limitName: 'pageSize'
            },
            page: true,
            //skin: 'line',
            //even: true, //开启隔行背景
            size: 'sm', //小尺寸的表格
            limit: '5',
            toolbar: '#tableToolBar',
            loading: true,
            height: 'full-160',
            cellMinWidth: 100,
            parseData: function (res) {
                return {
                    "code": 0, //解析接口状态
                    "msg": '', //解析提示文本
                    "count": res.total, //解析数据长度
                    "data": res.data //解析数据列表
                }
            },
            cols: [[
                {type: 'radio'},
                {
                    field: "title",
                    title: '名称',
                    width: 200,
                },
                {
                    field: "menus",
                    title: '拥有权限',
                    align: 'left',
                    templet: function (d) {
                        var tempstr = [];
                        $.each(d.menus, function (index, item) {
                            tempstr.push(item.title);
                        });
                        return tempstr.join(',');
                    }
                },
                {
                    field: 'status', width: 80, align: 'center',
                    title: '状态',
                    templet: function (d) {
                        if (d.status == 1) {
                            return '<span class="layui-badge layui-bg-blue">启用</span>';
                        } else {
                            return '<span class="layui-badge-rim">禁用</span>';
                        }
                    }
                },
                {
                    field: "remark",
                    title: '备注',
                    align: 'center',
                },
                {field: "", title: '操作', toolbar: "#operation", width: 220, fixed: 'right', align: 'center', hide: true},
            ]],
            done: function (res, curr, count) {
            }
        })
        table.on('toolbar(roleTable)', function (obj) {
            //console.log(obj);
            var data = table.checkStatus(obj.config.id).data[0];
            //console.log(data);
            if (obj.event == 'add') {
                $.get('{:url("/admin/sysrole/add")}', {}, function (str) {
                    var indexlayer = layui.layer.open({
                        title: "添加",
                        offset: 't',
                        area: ['530px', '500px'],
                        shadeClose: true,
                        type: 1,
                        anim: 2,
                        resize: false,
                        btn: ['保存', '取消'],
                        content: str,
                        yes: function (index, layero) {
                            //return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
                            var data = $("#roleForm").serializeObject();
                            //var param = dtree.getNowParam("parentIdTree");
                            var params = dtree.getCheckbarNodesParam("parentIdTree");
                            //console.log(params);
                            var menuIds = [];
                            $.each(params, function (index, item) {
                                menuIds.push(item.nodeId);
                            });
                            data.menuIds = menuIds;
                            $.ajax({
                                async: true,//默认true为异步
                                url: '{:url("/admin/sysrole/add")}',
                                type: "post",
                                data: data,
                                dataType: "json",
                                beforeSend: function (xhr) {
                                },
                                success: function (res, status, xhr) {
                                    layer.msg(res.msg);
                                    if (res.code == 1) {
                                        table.reload('roleTable', {});
                                        layui.layer.close(indexlayer);
                                    }
                                },
                                error: function (xhr, status, error) {
                                }
                            });
                        },
                        success: function (layero, index) {
                        },
                        btn2: function (index, layero) {
                            //按钮【按钮二】的回调
                            //return false 开启该代码可禁止点击该按钮关闭
                        },
                        btn3: function (index, layero) {
                            //按钮【按钮三】的回调
                            //return false 开启该代码可禁止点击该按钮关闭
                        },
                        cancel: function () {
                            //右上角关闭回调
                            //return false 开启该代码可禁止点击该按钮关闭
                        }
                    })
                });
            }
            if (obj.event == 'del') {
                if (data != null) {
                    layui.layer.confirm('确定删除此信息？', {icon: 2, title: '提示信息'}, function (index) {
                        $.ajax({
                            type: "post",
                            url: '{:url("/admin/sysrole/delete")}',
                            data: {'id': data.id},
                            dataType: 'json',
                            success: function (d) {
                                layui.layer.msg("删除成功");
                                table.reload('roleTable', {});
                            }
                        })
                        layer.close(index);
                    });
                } else {
                    layer.msg('请勾选记录！');
                }
            }
            if (obj.event == 'edit') {
                if (data != null) {
                    $.get('{:url("/admin/sysrole/edit")}', {"id": data.id}, function (str) {
                        var indexlayer = layui.layer.open({
                            title: "修改",
                            offset: 't',
                            area: ['530px', '500px'],
                            shadeClose: true,
                            type: 1,
                            anim: 2,
                            resize: false,
                            btn: ['保存', '取消'],
                            content: str,
                            yes: function (index, layero) {
                                //return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
                                var data = $("#roleForm").serializeObject();
                                //var param = dtree.getNowParam("parentIdTree");
                                var params = dtree.getCheckbarNodesParam("parentIdTree");
                                //console.log(params);
                                var menuIds = [];
                                $.each(params, function (index, item) {
                                    menuIds.push(item.nodeId);
                                });
                                data.menuIds = menuIds;
                                $.ajax({
                                    async: true,//默认true为异步
                                    url: '{:url("/admin/sysrole/edit")}',
                                    type: "post",
                                    data: data,
                                    dataType: "json",
                                    beforeSend: function (xhr) {
                                    },
                                    success: function (res, status, xhr) {
                                        layer.msg(res.msg);
                                        if (res.code == 1) {
                                            table.reload('roleTable', {});
                                            layui.layer.close(indexlayer);
                                        }
                                    },
                                    error: function (xhr, status, error) {
                                    }
                                });
                            },
                            success: function (layero, index) {
                            },
                            btn2: function (index, layero) {
                                //按钮【按钮二】的回调
                                //return false 开启该代码可禁止点击该按钮关闭
                            },
                            btn3: function (index, layero) {
                                //按钮【按钮三】的回调
                                //return false 开启该代码可禁止点击该按钮关闭
                            },
                            cancel: function () {
                                //右上角关闭回调
                                //return false 开启该代码可禁止点击该按钮关闭
                            }
                        })
                    });
                } else {
                    layer.msg('请勾选记录！');
                }
            }
        });
    });
</script>
</body>
</html>